package com.meida.boot.handler;

import com.meida.common.mybatis.entity.EntityMap;
import com.meida.common.mybatis.interceptor.GetInterceptor;
import com.meida.common.mybatis.query.CriteriaQuery;
import com.meida.common.utils.ApiAssert;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.FlymeUtils;
import com.meida.module.order.provider.service.OrderEvaluateService;
import com.meida.module.product.client.entity.*;
import com.meida.module.product.provider.service.*;
import com.meida.module.system.client.entity.SysCompany;
import com.meida.common.enums.ColleconEnum;
import com.meida.module.system.provider.service.SysColleconService;
import com.meida.module.system.provider.service.SysCompanyService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 产品详细信息处理器
 *
 * @author zyf
 */
@Component("productGetHandler")
@Log4j2
public class ProductGetHandler implements GetInterceptor {

    @Autowired
    private SysColleconService colleconService;


    @Autowired
    private ProdShopService shopService;

    @Autowired
    private OrderEvaluateService evaluateService;

    @Autowired
    private ProdProductService prodProductService;


    @Autowired
    private ProdMarketingService marketingService;

    @Autowired
    private ProdAftersaleService aftersaleService;
    @Autowired
    private SysCompanyService companyService;


    /**
     * 查询条件扩展
     *
     * @param cq
     * @return
     */
    @Override
    public void prepare(CriteriaQuery cq, EntityMap params) {
        cq.select(ProdProduct.class, "productId", "productName", "shopId", "productSeries", "productPrice", "oldPrice", "saleCount", "productScore", "coverImage","freight");
        cq.joinSelect(ProdShop.class, "shopLogo", "shopName");
    }


    /**
     * 返回结果集扩展
     *
     * @param cq
     * @return
     */
    @Override
    public void complete(CriteriaQuery cq, EntityMap map) {
        Long productId = map.getLong("productId");
        Long shopId = map.getLong("shopId");
        if (FlymeUtils.isNotEmpty(shopId)) {
            ProdShop shop = shopService.getById(shopId);
            //店铺收藏标记
            Integer shopScTag = colleconService.countCollecon(shopId, ColleconEnum.SC, ProdShop.class);
            map.put("shopScTag", shopScTag);
            //店铺收藏总数
            Integer shopScCount = colleconService.countAllCollecon(shopId, ColleconEnum.SC, ProdShop.class);
            map.put("shopScCount", shopScCount);
            //店铺评分
            map.put("shopScore", shop.getShopScore());
            //产品收藏标记
            Integer productScTag = colleconService.countCollecon(productId, ColleconEnum.SC, ProdProduct.class);
            map.put("productScTag", productScTag);
            SysCompany company=companyService.getById(shopId);
            if(FlymeUtils.isNotEmpty(company)){
                //即时通讯使用
                map.put("managerId",company.getManagerId());
            }
        }
        //查询产品评论
        List<EntityMap> commentList = evaluateService.listByCount(productId, 2);
        map.put("commentList", commentList);

        //查询最新推荐产品
        List<EntityMap> recommendList = prodProductService.listRecommend(10, false);
        map.put("recommendList", recommendList);

        Integer productType = cq.getInt("productType", 0);
        //营销商品详情参数
        if (!productType.equals(0)) {
            Long marketingId = cq.getLong("marketingId");
            ApiAssert.isNotEmpty("marketingId不能为空", marketingId);
            ProdMarketing marketing = marketingService.getById(marketingId);
            ApiAssert.isNotEmpty("营销商品不存在", marketing);
            map.put("beginTime", marketing.getBeginTime());
            //前端倒计时使用
            map.put("nowTime", DateUtils.getDateTime());
            map.put("endTime", marketing.getEndTime());
            map.put("marketingPrice", marketing.getMarketingPrice());
            map.put("productPrice", marketing.getProductPrice());
            map.put("marketingDesc", FlymeUtils.getString(marketing.getMarketingDesc(), "暂无信息"));
        }


        map.put("productType", productType);
        //TODO 商家优惠券数量
        int shopCouponCount = 1;
        map.put("shopCouponCount", shopCouponCount);
        List<EntityMap> afterSale = aftersaleService.selectByCompanyId(shopId);
        map.put("afterService", afterSale);

    }
}
